﻿// Author:					HungNM
// Created:					22-08-2012
// Last Modified:			22-08-2012
//

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;


namespace eProject.Web.UI
{

    public partial class UpdateInfoPage : System.Web.UI.Page
    {
        private string UserName = string.Empty;
        private string Password = string.Empty;
        private Users user;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadParams();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (IsPostBack) return;
            BindData();
        }

        private void BindData()
        {
            if (user == null) return;
            txtUserName.Text = user.UserName;
            txtFirstName.Text = user.FirstName;
            txtLastName.Text = user.LastName;
            txtDateOfBirth.Text = user.DateOfBirth.ToString("dd/MM/yyyy");
            txtEmail.Text = user.Email;
            txtEmail.Enabled = false;
            txtAddress.Text = user.Address;
            rdGender.SelectedValue = user.Gender.ToString();
            txtPhoneNumber.Text = user.PhoneNumber;
            txtDistance.Text = user.Distance.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;
            try
            {
                user = SiteUser.getCurrentUser();
                if (user == null)
                {
                    SessionClass.AuthRequestPage = Request.Path;
                    Response.Redirect("~/Login.aspx");
                }
                user.UserName = txtUserName.Text.Trim().ToString();
                user.FirstName = txtFirstName.Text.Trim().ToString();
                user.LastName = txtLastName.Text.Trim().ToString();
                user.DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text.Trim().ToString());
                user.Email = txtEmail.Text.Trim().ToString();
                user.Address = txtAddress.Text.Trim().ToString();
                user.Gender = (Convert.ToInt32(rdGender.SelectedValue) > 0);
                user.PhoneNumber = txtPhoneNumber.Text.Trim().ToString();
                //user.Roles = 1;
                if (user.Save())
                {
                    Result.Style["color"] = "Green";
                    ResultError.Text = "Update info success.";
                    Result.Visible = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                user = null;
            }
        }


        private void PopulateLabels()
        {
            if (IsPostBack) return;
            Title = "Update Info Page - eProject";
            ltHeader.Text = "Update Info";
            lblUser.Text = "User Name";
            lblEmail.Text = "Email";
            lblFirstName.Text = "First Name";
            lblLastName.Text = "Last Name";
            lblDateOfBirth.Text = "Date Of Birth";
            lblGender.Text = "Gender";
            lblAddress.Text = "Address";
            lblPhoneNumber.Text = "Phone Number";
            lblDistance.Text = "Distance";
        }

        private void LoadSettings()
        {
            if (IsPostBack) return;
            user = SiteUser.getCurrentUser();
            if (user == null)
            {
                SessionClass.AuthRequestPage = Request.Path;
                Response.Redirect("~/Login.aspx");
            }
        }

        private void LoadParams()
        {

        }

        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnSave.Click += new EventHandler(btnSave_Click);
        }

        #endregion
    }
}